package cn.kgc.watch.web;

import cn.kgc.watch.entity.Cart;
import cn.kgc.watch.entity.CartShopping;
import cn.kgc.watch.entity.Collect;
import cn.kgc.watch.entity.ResultInfo;
import cn.kgc.watch.entity.vo.PageInfoVo;
import cn.kgc.watch.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("commodity")
public class CommodityList {
    @Autowired
    private CommoditySpuService commoditySpuService;
    @Autowired
    private CartService cartService;
    @Autowired
    private CartShoppingService cartShoppingService;
    @Autowired
    private CommoditySkuService commoditySkuService;
    @Autowired
    private CollectService collectService;
    @GetMapping("indexShow")
    public ResultInfo indexShow(PageInfoVo pageInfoVo){
        return new ResultInfo(200,"成功",commoditySpuService.findPage(pageInfoVo));
    }
    //根据supId去找 sku
    @GetMapping("findById")
    public ResultInfo findById(Integer id){
        return new ResultInfo(200,"成功",this.commoditySpuService.findById(id));
    }
    //添加购物车查找是否有购物车
    @GetMapping("findCartSku")
    public ResultInfo findCartSku(Integer id){
        return new ResultInfo(200,"成功",this.cartService.findCartSku(id));
    }

    //如果没有购物车添加购物车
    @PostMapping("addCart")
    public ResultInfo addCart(@RequestBody Cart cart){
        this.cartService.addCart(cart);
        return new ResultInfo(200,"成功");
    }
    //如果有购物车添加商品
    @PostMapping("addCartShopping")
    public ResultInfo addCartShopping(@RequestBody CartShopping cartShopping){
        Long skuId = cartShopping.getCommoditySku().getId();
        Integer countSku = this.cartShoppingService.findCartShoppingById(Math.toIntExact(skuId));
        if(countSku!=0){
            this.cartShoppingService.modifyCartShopping(cartShopping);
        }else {
            this.cartShoppingService.addCartShopping(cartShopping);
        }
        return new ResultInfo(200,"成功");
    }
    //根据sukId查找sku
    @GetMapping("selectByIdSku")
    private ResultInfo selectByIdSku(Integer id){
        return new ResultInfo(200,"成功", Arrays.asList(this.commoditySkuService.findById(id)));
    }

    //查询是否有这个商品的收藏
    @GetMapping("findCollectById")
    public ResultInfo findCollectById(Integer id){
        return new  ResultInfo(200,"查询成功",this.collectService.findCollectById(id));
    }
    //添加收藏
    @PostMapping("addCollect")
    public ResultInfo addCollect(@RequestBody Collect collect){
        this.collectService.addCollect(collect);
        return new ResultInfo(200,"成功");
    }
}
